package org.openarmory.gui.domain;

import java.util.List;

import net.openarmory.core.domain.constants.ArmoryCharacterDataType;
import net.openarmory.core.domain.constants.ArmoryDataType;
import net.openarmory.core.domain.constants.ArmoryZone;
import net.openarmory.core.domain.validation.Realm;
import net.openarmory.core.domain.validation.RealmStatusRetriever;

import com.vaadin.data.Item;
import com.vaadin.data.validator.StringLengthValidator;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.Component;
import com.vaadin.ui.DefaultFieldFactory;
import com.vaadin.ui.Field;
import com.vaadin.ui.TextField;

public class ArmoryPageRetrieveFieldFactory extends DefaultFieldFactory {
	
	ComboBox zones = new ComboBox("Zone");
	ComboBox realms = new ComboBox("Realm");
	ComboBox dataTypes = new ComboBox("Data type");
	
	public ArmoryPageRetrieveFieldFactory(){
		zones.setWidth("5em");
		for (ArmoryZone zone:ArmoryZone.values()) {
			zones.addItem(zone);
		}
		
		realms.setWidth("12em");
		final RealmStatusRetriever rsr = new RealmStatusRetriever();
		final List<Realm> rsList = rsr.retrieveRealms();
		for (int i = 0; i < rsList.size(); i++) {
			realms.addItem(rsList.get(i).getServer());
				
		}
		
		dataTypes.setWidth("7em");
		for (ArmoryCharacterDataType characterDataType : ArmoryCharacterDataType.values()) {
			dataTypes.addItem(characterDataType);
			dataTypes.setItemCaption(characterDataType, characterDataType.getConsoleParameterValue());			
		}
		
	}
	
	@Override
	public Field createField(Item item, Object propertyId, Component uiContext) {
		Field f;
		if ("zone".equals(propertyId)) {
			return zones;	
		} else if("dataType".equals(propertyId)){
			return dataTypes;
		} else if("realm".equals(propertyId)){
			return realms;
		} else {
			f = super.createField(item, propertyId, uiContext);
		}
		
		if ("characterName".equals(propertyId)) {
        	TextField tf = (TextField) f;
        	tf.setRequired(true);
        	tf.setRequiredError("Please enter the name of you");
        	tf.setWidth("12em");
        	tf.addValidator(new StringLengthValidator(
                    "Character name must be between 1 and 17 characters long.", 1, 17, false));
        } 

        return f;
	}
	
}
